﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace TestApp
{
    public static class DRectangle 
    {
        private static Canvas parent;//父类的Canvas
        static bool isMove = false;//记录是否移动
        static Point startPoint;//记录鼠标移动的起始点
        static bool isStart = true;
        static Rectangle rg;
        static PointCollection pcTriangle; //记录图形需要的点     
        static double x = 0.0;
        static double y = 0.0;
        public static event EventHandler<DrawEventArgs> PointDrawed;
        //public DRectangle( )
        //{
        //  //  parent = cas;
        //  //  this.Loaded += new RoutedEventHandler(DRectangleLoad);
        //}

        //private void DRectangleLoad(object sender, RoutedEventArgs e)
        //{
        //    try
        //    {
        //        parent = this.Parent as Canvas;
        //    }
        //    catch (Exception)
        //    {
        //        throw new Exception("DRectangle 的父元素应该指定为Canvas类型！");
        //    }
        //    parent.MouseLeftButtonDown += new MouseButtonEventHandler(Handle_MouseLeftButtonDown);
        //    parent.MouseMove += new MouseEventHandler(Handle_MouseMove);
        //    parent.MouseLeftButtonUp += new MouseButtonEventHandler(Handle_MouseLeftButtonUp);
        //}

        public static void Play(Canvas pare) {
            parent = pare;
            parent.MouseLeftButtonDown += new MouseButtonEventHandler(Handle_MouseLeftButtonDown);
            parent.MouseMove += new MouseEventHandler(Handle_MouseMove);
            parent.MouseLeftButtonUp += new MouseButtonEventHandler(Handle_MouseLeftButtonUp);
        }


        private static void Handle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            element.ReleaseMouseCapture();
            isMove = false;
            isStart = true;
            DrawEventArgs args = new DrawEventArgs();

            PointDrawed(sender, args);

        }

        private static void rg_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

            Rectangle item = sender as Rectangle;
            isStart = true;
            item.ReleaseMouseCapture();
            //DrawEventArgs args = new DrawEventArgs();

            //PointDrawed(sender, args);
        }

        private static void rg_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //e.Handled = true;
            Rectangle item = sender as Rectangle;
            y = e.GetPosition(null).Y;
            x = e.GetPosition(null).X;
            //isMove = true;
            item.CaptureMouse();
            isStart = true;
            item.Stroke = new SolidColorBrush(Colors.Black);
            item.StrokeThickness = 3;
           
        }

        private static void rg_Move(object sender, MouseEventArgs e)
        {
            Rectangle item = sender as Rectangle;

            if (isMove)
            {
                // Calculate the current position of the object. 
                double deltaV = e.GetPosition(null).Y - y;
                double deltaH = e.GetPosition(null).X - x;
                double newTop = deltaV + (double)item.GetValue(Canvas.TopProperty);
                double newLeft = deltaH + (double)item.GetValue(Canvas.LeftProperty);
                // Set new position of object. 
                item.SetValue(Canvas.TopProperty, newTop);
                item.SetValue(Canvas.LeftProperty, newLeft);
                // Update position global variables.  
                y = e.GetPosition(null).Y;
                x = e.GetPosition(null).X;
                isStart = true;
            }
        }
        #region 创建右键菜单
        private static void CreatMenu(Rectangle rect)
        {
            ContextMenu cm = new ContextMenu();
            MenuItem m1 = new MenuItem();
            m1.Header = "复制";
            m1.Click += new RoutedEventHandler(Copy_Click);
            cm.Items.Add(m1);

            MenuItem m2 = new MenuItem();
            m2.Header = "修改";
            m2.Click += new RoutedEventHandler(Modify_Click);
            cm.Items.Add(m2);
            MenuItem m3 = new MenuItem();
            m3.Header = "删除";
            m3.Click += new RoutedEventHandler(Del_Click);
            cm.Items.Add(m3);

            ContextMenuService.SetContextMenu(rect, cm);
        }

        private static void Del_Click(object sender, RoutedEventArgs e)
        {
            
            parent.Children.RemoveAt(1);
        }

         /// <summary>
        /// 在图片上点击修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Modify_Click(object sender, RoutedEventArgs e)
        {

        }

        private static void Copy_Click(object sender, RoutedEventArgs e)
        {

        }
        #endregion


        static void Handle_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMove)
            {
                if (!isStart)
                {
                    rg.Width = Math.Abs(e.GetPosition(parent).X - startPoint.X);
                    rg.Height = Math.Abs(e.GetPosition(parent).Y - startPoint.Y);
                }
                
                pcTriangle.Add(e.GetPosition(parent));
                pcTriangle.Add(new Point(startPoint.X, e.GetPosition(parent).Y));
                pcTriangle.Add(startPoint);

            }
        }

        private static void Handle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            FrameworkElement element = sender as FrameworkElement;
            bool flg = element.CaptureMouse();
            if (isStart)
            {
                rg = new Rectangle()
                {
                    Fill = new SolidColorBrush(Colors.Red),
                    Name = Guid.NewGuid().ToString(),
                    RadiusX = 10,
                    RadiusY = 10,
                    AllowDrop = true
                };
                //鼠标选中方块移动
                rg.MouseMove += new MouseEventHandler(rg_Move);
                rg.MouseLeftButtonDown += new MouseButtonEventHandler(rg_MouseLeftButtonDown);
                //鼠标释放 方块位置不动
                rg.MouseLeftButtonUp += new MouseButtonEventHandler(rg_MouseLeftButtonUp);
                CreatMenu(rg);
                //rg.DragLeave +=rg_DragLeave;
                startPoint = e.GetPosition(parent);
                pcTriangle = new PointCollection();
                pcTriangle.Add(startPoint);
                rg.SetValue(Canvas.LeftProperty, startPoint.X);
                rg.SetValue(Canvas.TopProperty, startPoint.Y);
                parent.Children.Add(rg);
                isStart = false;
                isMove = true;
            }
        }

    }
}
